<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once('backend.php');

class Admin extends Backend {

    protected $path = 'admin';

    function Admin() {
        parent::__construct();
    }

    public function do_upload($name_folder) {

        $config['upload_path'] = './htdocs/images/' . $name_folder . '/';
        $config['allowed_types'] = 'gif|jpg|png';
        //$config['max_size'] = '1000';
        //$config['max_width'] = '1024';
        // $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('hinhanh')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $this->upload->data();
            $config['image_library'] = 'gd2';
            $config['source_image'] = $config['upload_path'] . '/' . $_FILES['hinhanh']['name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = 122;
            $config['height'] = 162;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
        }
    }

    public function index($page = null, $action = null, $subAction = null, $item = null) {

        switch ($page) {
            case 'tin-tuc':
                $this->Tintuc($page, $action, $subAction, $item);
                break;
            case 'nha-dat':
                $this->Nhadat($page, $action, $subAction, $item);
                break;
            case 'noi-ngoai-that':
                $this->Noingoaithat($page, $action, $subAction, $item);
                break;
            case 'login':
                $this->Login($page, $action, $subAction, $item);
                break;
            case 'du-an':
                $this->Duan($page, $action, $subAction, $item);
                break;
            case 'nha-mau':
                $this->Nhamau($page, $action, $subAction, $item);
                break;
            case 'trang-chu':
                $this->Trangchu($page, $action, $subAction, $item);
                break;
            case 'lien-he':
                $this->Lienhe($page, $action, $subAction, $item);
                break;
            case 'gioi-thieu':
                $this->Gioithieu($page, $action, $subAction, $item);
                break;
            default :
                break;
        }
    }

    public function Trangchu($page = null, $action = null, $subAction = null, $item = null) {
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/trang-chu';
    }

    public function Login($page = null, $action = null, $subAction = null, $item = null) {
        $this->template = 'login';
        if ($action == 'submit') {
            $this->load->model('muser');
            $user = $this->input->post('username');
            $pass = $this->input->post('password');
            $res = $this->muser->IdentifyUser($user);
            if ($res['password'] == $this->input->post('password')) {
                $data = array('username' => $user,
                );
                $this->session->set_userdata($data);
                redirect('admin/trang-chu');
            } else {
                redirect('admin/login');
            }
        }
        if ($action == 'sign-out') {
            if ($this->session->userdata('username') != false) {
                $this->session->unset_userdata('username');
            }
            redirect('admin/login');
        }
    }

    public function Tintuc($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Tin tức';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_tin_tuc = $this->mtintuc->findAllNews();
        for ($i = 0; $i < count($list_tin_tuc); $i++) {
            $list_tin_tuc[$i]['created_date'] = $this->fomatDateForm($list_tin_tuc[$i]['created_date']);
        }
        $this->data['list_news'] = $list_tin_tuc;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/tin-tuc';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $this->do_upload('tintucs');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/tintucs/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addTinTuc($data);
                    redirect('admin/tin-tuc');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-tin-tuc';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateNews($data, $check[$i]);
                    }
                    redirect('admin/tin-tuc');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $this->do_upload('tintucs');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/tintucs/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateNews($data, $item);
                    redirect('admin/tin-tuc');
                }
                $list_detail = $this->mtintuc->getInforNewsById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-tin-tuc';
                break;
            default :
                break;
        }
    }

    public function Nhadat($page = null, $action = null, $subAction = null, $item = null) {
            $select_menu = 'Nhà đất';
            $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_nha_dat = $this->mtintuc->findAllNhadat();
        for ($i = 0; $i < count($list_nha_dat); $i++) {
            $list_nha_dat[$i]['created_date'] = $this->fomatDateForm($list_nha_dat[$i]['created_date']);
        }
        $this->data['list_nha_dat'] = $list_nha_dat;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/nha-dat';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $this->do_upload('nhadats');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/nhadats/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addNhadat($data);
                    redirect('admin/nha-dat');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-nha-dat';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateNhadat($data, $check[$i]);
                    }
                    redirect('admin/nha-dat');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $this->do_upload('nhadats');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/nhadats/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateNhadat($data, $item);
                    redirect('admin/nha-dat');
                }
                $list_detail = $this->mtintuc->getInforNhadatById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-nha-dat';
                break;
            default :
                break;
        }
    }
  public function Noingoaithat($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Nội ngoại thất';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_noi_that = $this->mtintuc->findAllNoithat();
        for ($i = 0; $i < count($list_noi_that); $i++) {
            $list_noi_that[$i]['created_date'] = $this->fomatDateForm($list_noi_that[$i]['created_date']);
        }
        $this->data['list_noi_that'] = $list_noi_that;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/noi-ngoai-that';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $this->do_upload('noithats');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/noithats/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addNoithat($data);
                    redirect('admin/noi-ngoai-that');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-noi-ngoai-that';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateNoithat($data, $check[$i]);
                    }
                    redirect('admin/noi-ngoai-that');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $this->do_upload('noithats');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'url_image' => 'images/nhadats/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateNoithat($data, $item);
                    redirect('admin/noi-ngoai-that');
                }
                $list_detail = $this->mtintuc->getInforNoithatById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-noi-ngoai-that';
                break;
            default :
                break;
        }
    }
    public function Gioithieu($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Giới thiệu';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_noi_that = $this->mtintuc->findAllGioithieu();
        for ($i = 0; $i < count($list_noi_that); $i++) {
            $list_noi_that[$i]['created_date'] = $this->fomatDateForm($list_noi_that[$i]['created_date']);
        }
        $this->data['list_noi_that'] = $list_noi_that;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/gioi-thieu';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $data = array(
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addGioithieu($data);
                    redirect('admin/gioi-thieu');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-gioi-thieu';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateGioithieu($data, $check[$i]);
                    }
                    redirect('admin/gioi-thieu');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $data = array(
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateNoithat($data, $item);
                    redirect('admin/gioi-thieu');
                }
                $list_detail = $this->mtintuc->getInforGioithieuById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-gioi-thieu';
                break;
            default :
                break;
        }
    }
    
     public function Duan($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Dự án';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_du_an = $this->mtintuc->findAllDuan();
        for ($i = 0; $i < count($list_du_an); $i++) {
            $list_du_an[$i]['created_date'] = $this->fomatDateForm($list_du_an[$i]['created_date']);
        }
        $this->data['list_du_an'] = $list_du_an;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/du-an';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $this->do_upload('duans');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'type' => $this->input->post('select-duan'),
                        'url_image' => 'images/duans/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addDuan($data);
                    redirect('admin/du-an');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-du-an';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateDuan($data, $check[$i]);
                    }
                    redirect('admin/du-an');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $this->do_upload('duans');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'type' => $this->input->post('select-duan'),
                        'url_image' => 'images/duans/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateDuan($data, $item);
                    redirect('admin/du-an');
                }
                $list_detail = $this->mtintuc->getInforDuanById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-du-an';
                break;
            default :
                break;
        }
    }
     public function Nhamau($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Nhà mẫu';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_du_an = $this->mtintuc->findAllNhamau();
        for ($i = 0; $i < count($list_du_an); $i++) {
            $list_du_an[$i]['created_date'] = $this->fomatDateForm($list_du_an[$i]['created_date']);
        }
        $this->data['list_du_an'] = $list_du_an;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/nha-mau';
        switch ($action) {
            case 'add':
                if ($subAction == 'submit') {
                    $this->do_upload('nhamaus');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'type' => $this->input->post('select-duan'),
                        'url_image' => 'images/nhamaus/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->addNhamau($data);
                    redirect('admin/nha-mau');
                }
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/add-nha-mau';
                break;
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateNhamau($data, $check[$i]);
                    }
                    redirect('admin/nha-mau');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    $this->do_upload('nhamaus');
                    $data = array(
                        'title' => $this->input->post('title'),
                        'type' => $this->input->post('select-duan'),
                        'url_image' => 'images/nhamaus/' . $_FILES['hinhanh']['name'],
                        'sumary' => $this->input->post('tom-tat'),
                        'content' => $this->input->post('editor1'),
                        'created_date' => date('Y-m-d H:i:s'),
                        'status' => 'active'
                    );
                    $this->mtintuc->updateNhamau($data, $item);
                    redirect('admin/nha-mau');
                }
                $list_detail = $this->mtintuc->getInforNhamauById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-nha-mau';
                break;
            default :
                break;
        }
    }
        public function Lienhe($page = null, $action = null, $subAction = null, $item = null) {
        $select_menu = 'Liên hệ';
        $this->data['selected'] = $select_menu;
        if ($this->session->userdata('username') == false) {
            redirect('admin/login');
        }
        $this->load->model('mtintuc');
        $this->load->helper('date');
        $this->load->helper('text');
        $list_tin_tuc = $this->mtintuc->findAllLienhe();
        for ($i = 0; $i < count($list_tin_tuc); $i++) {
            $list_tin_tuc[$i]['created_date'] = $this->fomatDateForm($list_tin_tuc[$i]['created_date']);
        }
        $this->data['list_news'] = $list_tin_tuc;
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/lien-he';
        switch ($action) {
            case 'delete':
                if ($subAction == 'submit') {
                    $check = $this->input->post('check');
                    for ($i = 0; $i < count($check); $i++) {
                        $data = array(
                            'status' => 'delete'
                        );
                        $this->mtintuc->updateLienhe($data, $check[$i]);
                    }
                    redirect('admin/lien-he');
                }
                break;
            case 'edit':
                if ($subAction == 'submit') {
                    redirect('admin/lien-he');
                }
                $list_detail = $this->mtintuc->getInforLienheById($item);
                $this->data['list_detail'] = $list_detail;
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'admin/edit-lien-he';
                break;
            default :
                break;
        }
    }
    public function fomatDateForm($date) {
        if ($date != '') {
            $formatDate = preg_replace("![^0-9]+!i", '-', $date);
            $expdate = explode('-', $formatDate);
            $dateDatabase = $expdate[2] . '/' . $expdate[1] . '/' . $expdate[0];
            //$newDate = $dateDatabase->format('m-d-Y');
            return $dateDatabase;
        }
    }

    public function fomatDateDatabase($date) {
        if ($date != '') {
            $formatDate = preg_replace("![^0-9]+!i", '-', $date);
            $expdate = explode('-', $formatDate);
            $dateDatabase = new DateTime($expdate[2] . '-' . $expdate[0] . '-' . $expdate[1]);
            $newDate = $dateDatabase->format('Y-m-d');
            return $newDate;
        }
    }

}